package com.ars.common.servlet;

import com.ars.common.beans.User.User;
import com.ars.common.dao.UserInfo.FindUserById;
import com.ars.common.utils.JSONUtil;

import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.PrintWriter;
import java.sql.SQLException;

/**
 * ClassName: meServlet
 * Package: com.ars.common.servlet
 * Description:
 *
 * @Author: Bryan Long
 * @Create: 2024/8/29 - 14:32
 * @Version: v1.0
 */
@WebServlet("/me")
public class meServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;

    @Override
    public void doGet(HttpServletRequest request, HttpServletResponse response){
        System.out.println("me servlet");

        String userId = request.getParameter("user_id");
        System.out.println("userId:"+userId);

        try {
            // 调用dao获取用户信息
            User user = new FindUserById().findUserById(userId);
            // 转换成JSON格式字符串
            String userInfo = JSONUtil.parseJson(user);

            response.setContentType("application/json");
            response.setCharacterEncoding("UTF-8");
            PrintWriter out = response.getWriter();
            out.print(userInfo);
            out.flush();
        } catch (SQLException e) {
            throw new RuntimeException(e);
        } catch (IOException e) {
            throw new RuntimeException(e);
        }

    }

    @Override
    public void doPost(HttpServletRequest request, HttpServletResponse response){
        doGet(request, response);
    }
}
